using System;
using System.Reflection;
using System.Reflection.Emit;

namespace SDILReader
{

    public class ILInstruction
    {
        private OpCode code;

        private object operand;

        private byte[] operandData;

        private int offset;

        public OpCode Code
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
            }
        }

        public object Operand
        {
            get
            {
                return operand;
            }
            set
            {
                operand = value;
            }
        }

        public byte[] OperandData
        {
            get
            {
                return operandData;
            }
            set
            {
                operandData = value;
            }
        }

        public int Offset
        {
            get
            {
                return offset;
            }
            set
            {
                offset = value;
            }
        }

        public string GetCode()
        {
            string text = "";
            object obj = text;
            text = string.Concat(obj, GetExpandedOffset(offset), " : ", code);
            if (operand != null)
            {
                switch (code.OperandType)
                {
                    case OperandType.InlineField:
                        {
                            FieldInfo fieldInfo = (FieldInfo)operand;
                            string text2 = text;
                            text = text2 + " " + Globals.ProcessSpecialTypes(fieldInfo.FieldType.ToString()) + " " + Globals.ProcessSpecialTypes(fieldInfo.ReflectedType.ToString()) + "::" + fieldInfo.Name + "";
                            break;
                        }
                    case OperandType.InlineMethod:
                        try
                        {
                            MethodInfo methodInfo = (MethodInfo)operand;
                            text += " ";
                            if (!methodInfo.IsStatic)
                            {
                                text += "instance ";
                            }
                            string text2 = text;
                            text = text2 + Globals.ProcessSpecialTypes(methodInfo.ReturnType.ToString()) + " " + Globals.ProcessSpecialTypes(methodInfo.ReflectedType.ToString()) + "::" + methodInfo.Name + "()";
                        }
                        catch
                        {
                            try
                            {
                                ConstructorInfo constructorInfo = (ConstructorInfo)operand;
                                text += " ";
                                if (!constructorInfo.IsStatic)
                                {
                                    text += "instance ";
                                }
                                string text2 = text;
                                text = text2 + "void " + Globals.ProcessSpecialTypes(constructorInfo.ReflectedType.ToString()) + "::" + constructorInfo.Name + "()";
                            }
                            catch
                            {
                            }
                        }
                        break;
                    case OperandType.InlineBrTarget:
                    case OperandType.ShortInlineBrTarget:
                        text = text + " " + GetExpandedOffset((int)operand);
                        break;
                    case OperandType.InlineType:
                        text = text + " " + Globals.ProcessSpecialTypes(operand.ToString());
                        break;
                    case OperandType.InlineString:
                        text = ((!(operand.ToString() == "\r\n")) ? (text + " \"" + operand.ToString() + "\"") : (text + " \"\\r\\n\""));
                        break;
                    case OperandType.ShortInlineVar:
                        text += operand.ToString();
                        break;
                    case OperandType.InlineI:
                    case OperandType.InlineI8:
                    case OperandType.InlineR:
                    case OperandType.ShortInlineI:
                    case OperandType.ShortInlineR:
                        text += operand.ToString();
                        break;
                    case OperandType.InlineTok:
                        text = ((!(operand is Type)) ? (text + "not supported") : (text + ((Type)operand).FullName));
                        break;
                    default:
                        text += "not supported";
                        break;
                }
            }
            return text;
        }

        private string GetExpandedOffset(long offset)
        {
            string text = offset.ToString();
            int num = 0;
            while (text.Length < 4)
            {
                text = "0" + text;
                num++;
            }
            return text;
        }
    }

}